package hdd.config;

import freemarker.template.utility.XmlEscape;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import web.view.CutomerFreemarkerView;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan(basePackages = {"hdd.controller"})
@PropertySource({"classpath:freemarkerSetting.properties"})
public class ServletConfig extends WebMvcConfigurerAdapter implements EnvironmentAware {

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    private Environment environment;

    //=================freemarker的配置 ===============
    @Bean
    public XmlEscape xmlEscape() {
        return new XmlEscape();
    }

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer(XmlEscape xmlEscape) {
        FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
        freeMarkerConfigurer.setTemplateLoaderPath( "/" );//
        freeMarkerConfigurer.setDefaultEncoding( environment.getProperty( "default_encoding" ) );
        Map map = new HashMap();
        map.put( "xml_escape", xmlEscape );
        freeMarkerConfigurer.setFreemarkerVariables( map );
        Properties properties = new Properties();
        properties.setProperty( "locale", environment.getProperty( "locale", "zh_CN" ) );
        properties.setProperty( "template_update_delay", environment.getProperty( "template_update_delay" ) );
        properties.setProperty( "datetime_format", environment.getProperty( "datetime_format" ) );//时间格式化
        properties.setProperty( "date_format", environment.getProperty( "date_format" ) );
        properties.setProperty( "number_format", environment.getProperty( "number_format" ) );
        properties.setProperty( "default_encoding", environment.getProperty( "default_encoding" ) );
        properties.setProperty( "output_encoding", environment.getProperty( "output_encoding" ) );
        properties.setProperty( "classic_compatible", environment.getProperty( "classic_compatible" ) );
        properties.setProperty( "auto_import", "spring.ftl as spring,WEB-INF/views/commons/main.ftl as main" ); //自动导入的内容
        freeMarkerConfigurer.setFreemarkerSettings( properties );
        return freeMarkerConfigurer;
    }

    //解析html 视图
    @Bean
    public FreeMarkerViewResolver htmlFreeMarkerViewResolver() {
        FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver( "WEB-INF/views", ".html" );
        freeMarkerViewResolver.setOrder( 2 );
        freeMarkerViewResolver.setExposeContextBeansAsAttributes( true );
        freeMarkerViewResolver.setContentType( "text/html;charset=UTF-8" );
        freeMarkerViewResolver.setAllowRequestOverride( true );
        freeMarkerViewResolver.setAllowSessionOverride( true );
        freeMarkerViewResolver.setExposeSpringMacroHelpers( true );
        freeMarkerViewResolver.setExposeContextBeansAsAttributes( true );
        freeMarkerViewResolver.setExposePathVariables( true );
        freeMarkerViewResolver.setExposeRequestAttributes( true );
        freeMarkerViewResolver.setExposeSessionAttributes( true );
        freeMarkerViewResolver.setRequestContextAttribute( "request" );
        freeMarkerViewResolver.setCache( true );
        freeMarkerViewResolver.setViewClass( CutomerFreemarkerView.class );
        return freeMarkerViewResolver;
    }

    //解析freemarker 视图
    @Bean
    public ViewResolver freeMarkerViewResolver() {
        FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver( "/WEB-INF/views/", ".ftl" );
        freeMarkerViewResolver.setOrder( 3 );
        freeMarkerViewResolver.setContentType( "text/html;charset=UTF-8" );
        freeMarkerViewResolver.setAllowRequestOverride( true );
        freeMarkerViewResolver.setAllowSessionOverride( true );
        freeMarkerViewResolver.setExposeSpringMacroHelpers( true );
        freeMarkerViewResolver.setExposeContextBeansAsAttributes( true );
        freeMarkerViewResolver.setExposePathVariables( true );
        freeMarkerViewResolver.setExposeRequestAttributes( true );
        freeMarkerViewResolver.setExposeSessionAttributes( true );
        freeMarkerViewResolver.setRequestContextAttribute( "request" );
        freeMarkerViewResolver.setCache( true );
        freeMarkerViewResolver.setViewClass( CutomerFreemarkerView.class );
        return freeMarkerViewResolver;
    }


    // 解析 jsp视图
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver( "/WEB-INF/views/", ".jsp" );
        internalResourceViewResolver.setExposeContextBeansAsAttributes( true );
        internalResourceViewResolver.setOrder( 4 );
        return internalResourceViewResolver;
    }


    // 创建 ContentNegotiationConfigurer bean
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        super.configureContentNegotiation( configurer );
        configurer.defaultContentType( MediaType.APPLICATION_JSON_UTF8 );
    }

    //内容协商 视图解析器
    //视图配置 在list中按照配置的先后顺序
    @Bean
    public ContentNegotiatingViewResolver contentNegotiatingViewResolver(ContentNegotiationManager contentNegotiationConfigurer) {
        ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();
        contentNegotiatingViewResolver.setOrder( 1 );
        contentNegotiatingViewResolver.setContentNegotiationManager( contentNegotiationConfigurer );
        return contentNegotiatingViewResolver;
    }


    //配置文件上传，如果没有使用文件上传可以不用配置，当然如果不配，那么POM文件中也不必引入依赖
    //如果controller中的handler 用part接口接收上传文件的话，可以不必设置此解析器
    @Bean
    public CommonsMultipartResolver commonsMultipartResolver() {
        CommonsMultipartResolver commonsMultipartResolve = new CommonsMultipartResolver();
        commonsMultipartResolve.setDefaultEncoding( "UTF-8" );
        return commonsMultipartResolve;
    }


    //  属性源占位符
    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertySourcesPlaceholderConfigurer.setFileEncoding( "UTF-8" );
        return propertySourcesPlaceholderConfigurer;
    }


    //对静态资源的处理
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        super.configureDefaultServletHandling( configurer );
        configurer.enable();
    }


    //搭建了一个资源处理器，
    //让它负责解析路径以“/webjars/**”开头的请求，这也是WebJars的标准路径
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        super.addResourceHandlers( registry );
//        registry.addResourceHandler( "/webjars/**" ).addResourceLocations( "classpath:/META-INF/resourves/webjars" );
//    }
}
